package com.silence.whereeat.utils;

import com.silence.whereeat.model.UserInfo;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * httpClientPost
 * Created by 伟阳 on 2015/9/16.
 */
public class HttpClientPost {
    private String URL_PATH = "";
    private HttpClient client;
    private HttpPost httpPost;

    public HttpClientPost(String urlPath) {
        this.URL_PATH = urlPath;
        client = new DefaultHttpClient();
        httpPost = new HttpPost(URL_PATH);
    }

    public String getLoginResult(UserInfo userInfo) {
        String result = "";
        List<BasicNameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("username", userInfo.getUser_name()));
        list.add(new BasicNameValuePair("password", userInfo.getUser_password()));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(list));
            HttpResponse response = client.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getFoods(int passage) {
        String result = "";
        List<BasicNameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("passage", passage + ""));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(list));
            HttpResponse response = client.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "UTF_8").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
